package com.hs.controller;

import com.hs.VO.LessonPO;
import com.hs.VO.LessonStudentVO;
import com.hs.config.MyValues;
import com.hs.config.R;
import com.hs.entity.*;
import com.hs.service.ClassLessonIssueService;
import com.hs.service.ClassLessonService;
import com.hs.service.LessonService;
import com.hs.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("teacher")
public class TeacherController {

    @Autowired
    StudentService studentService;
    @Autowired
    LessonService lessonService;
    @Autowired
    ClassLessonIssueService classLessonIssueService;
    @Autowired
    ClassLessonService classLessonService;
    /**
     *  teacher module
     */
    @RequestMapping("/lesson")
    public String lesson(HttpSession session, Model model, Lesson record){
        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("taskValue",new MyValues());
        if(record.getPageSize() == null){
            record.setPageSize(8);
        }
        if(record.getCurrentPage() == null ){
            record.setCurrentPage(0);
        }
        if(record.getFlag() == null){
            record.setFlag(0);
        }
        User user = (User) session.getAttribute("user");
        record.setUserId(user.getId());
        record.setCurrentPage(record.getCurrentPage() + record.getFlag());
        record.setPageStart(record.getCurrentPage() *  record.getPageSize());
        model.addAttribute("page",lessonService.page(record));
        return "teacher/lesson_list";
    }
    /**
     *   user   classId  通过班级和classId确定教师的课程列表
     * @param session
     * @param model
     * @param record classId
     * @return
     */
    @RequestMapping("/classLessonList")
    public String classLessonList(HttpSession session, Model model, LessonPO record){
        User user =(User) session.getAttribute("user");
        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        record.setUserId(user.getId());
        model.addAttribute("list",lessonService.selectTeacherLesson(record));
    //        //弹窗之后的获取值  调用方法用 。。。。。。 来回传啊     ！！！！！！！！！！有没有好方法  。。。。。待会学生列表也能用到。。。哦 就在下面 一行的方法 classStudentList
    // 废弃了 谢谢
        return "teacher/class_lesson_list";
    }
    @RequestMapping("/classStudentList")
    public String classStudentList(HttpSession session, Model model, Student record){
        User user =(User) session.getAttribute("user");
        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("list",studentService.selectByCondition(record));
        return "teacher/class_student_list";
    }



    @RequestMapping("activeLesson")
    @ResponseBody
    public R activeLesson(@RequestParam Integer id) {
        if(id == null){
            return R.FAILURE("用户id不能为空",null);
        }
        ClassLesson classLesson = new ClassLesson();
        classLesson.setStatus(1);
        classLesson.setId(id);
        int i = classLessonService.updateByPrimaryKeySelective(classLesson);
        if(i > 0){
            return R.SUCCESS("",i);
        }
        return R.FAILURE("用户更新失败",0);
    }

    @RequestMapping("removeStudentClass")
    @ResponseBody
    public R removeStudentClass(@RequestParam Integer id) {
        if(id == null){
            return R.FAILURE("用户id不能为空",null);
        }

        int i = studentService.removeStudentClass(id);
        if(i > 0){
            return R.SUCCESS("",i);
        }
        return R.FAILURE("用户更新失败",0);
    }


}
